@echo off
cls

:: Installation script for the build platform
set WORKSPACE_FOLDER=E:\build\workspace

echo.
echo ################################# INIT #################################
echo "Installing JAVA JDK"
echo.
echo "Starting installation"

:: Catch Time when beginning installation script
for /f "tokens=1 delims=: " %%h in ('echo %time%') do set start_hour=%%h 2>NULL
for /f "tokens=2 delims=: " %%m in ('echo %TIME%') do set start_minu=%%m 2>NULL
for /f "tokens=3 delims=: " %%s in ('echo %TIME%') do set start_secd=%%s 2>NULL

:: Get Root folder (ie c:\, d:\, e:\)
SET mydir="%WORKSPACE_FOLDER%"
SET mydir=%mydir:\=;%
for /F "tokens=* delims=;" %%i IN (%mydir%) DO CALL :PARENT_FOLDER %%i
:PARENT_FOLDER
if "%1"=="" (GOTO :EOF)
SET ROOT=%1

:: Global data
set TOOLS_FOLDER=%ROOT%\tools
set JAVA_FOLDER=%ROOT%\java
set WGET_FOLDER=%TOOLS_FOLDER%\wget
set UNZIP_FOLDER=%TOOLS_FOLDER%\unzip

echo "Installation will be done in root folder........... : %ROOT%\"
echo "Installation of java will be done in folder........ : %JAVA_FOLDER%\"
echo "Installation of tools will be done in folder....... : %TOOLS_FOLDER%\"


:: Jdk data
set JDK_URL=http://gpssys.googlecode.com/files/jdk-6u21-windows-i586.exe
set JDK_LogFile=%TOOLS_FOLDER%\jdk.log
set JDK_InstallDIR=%JAVA_FOLDER%\jdk1.6.0_21
set JDK_MSIPKG=C:\Documents and Settings\%username%\Application Data\Sun\Java\jdk1.6.0_21\jdk1.6.0_21.msi
set JAVA_HOME=%JDK_InstallDIR%
set JAVA_BinDIR=%JAVA_HOME%\bin

:: Unzip data
set UNZIP_BINDIR=%TOOLS_FOLDER%\unzip\bin

:: Wget data
set WGET_BINDIR=%TOOLS_FOLDER%\wget\bin
set WGET_BINZIP=wget-1.11.4-1-bin.zip
set WGET_DEPZIP=wget-1.11.4-1-dep.zip

echo ################################# INIT #################################

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF BASIC STUFF 
echo **********************************************************************
echo.
echo "Uninstalling jdk if already installed"
if exist "%JDK_MSIPKG%" msiexec /x "%JDK_MSIPKG%" /qn
echo Ok.

echo.
echo "Cleaning JAVA + Tools folders"
if exist "%TOOLS_FOLDER%" rmdir /Q /S "%TOOLS_FOLDER%" 2>NULL
if exist "%UNZIP_FOLDER%" rmdir /Q /S "%UNZIP_FOLDER%" 2>NULL
if exist "%JAVA_FOLDER%"  rmdir /Q /S "%JAVA_FOLDER%" 2>NULL
echo Ok.

echo.
echo "Creating MySQL + Maven + Java + Tools folders"
mkdir "%TOOLS_FOLDER%" 2>NULL
mkdir "%WGET_FOLDER%" 2>NULL
mkdir "%UNZIP_FOLDER%" 2>NULL
mkdir "%UNZIP_BINDIR%" 2>NULL
mkdir "%JAVA_BINDIR%" 2>NULL
echo Ok.


echo.
echo **********************************************************************
echo INSTALLATION OF UNZIP
echo **********************************************************************

echo.
echo "Copying needed tools (unzip)"
copy "unzip.exe" "%UNZIP_BINDIR%" 2>NULL

echo "According Global PATH variable for unzip"
set PATH=%PATH%;%UNZIP_BINDIR%
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF WGET
echo **********************************************************************

echo.
echo "Copying needed tools (wget)"
copy "%WGET_BINZIP%" "%TOOLS_FOLDER%" 2>NULL
copy "%WGET_DEPZIP%" "%TOOLS_FOLDER%" 2>NULL
echo Ok.

cd %ROOT%
%ROOT%
cd "%WGET_FOLDER%"

echo.
echo "Unziping/Installing wget command"
unzip -qq "..\%WGET_BINZIP%"
unzip -qq "..\%WGET_DEPZIP%"

echo "According Global PATH variable for wget"
set PATH=%PATH%;%WGET_BINDIR%
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo.
echo **********************************************************************
echo INSTALLATION OF JDK
echo **********************************************************************
cd "%TOOLS_FOLDER%"
echo.
echo "Catching JDK exe package"
wget.exe "%JDK_URL%"

echo.
echo "Installing JDK package (jdk-6u21-windows-i586.exe - log is %JDK_LogFile%)"
"jdk-6u21-windows-i586.exe" /l* "%JDK_LogFile%" INSTALLDIR="%JDK_InstallDIR%" /qn
echo Ok.

echo.
echo "According Global PATH variable"
set PATH=%PATH%;%JAVA_BinDIR%
echo Ok.

echo.
echo "Checking java version"
java -version
echo Ok.

::Pause 2s
ping 127.0.0.1 -n 2 >NULL


echo **********************************************************************
echo EXECUTION OF FINAL CLEAN UP
echo **********************************************************************
echo.
echo "Removing zip files"
del "%TOOLS_FOLDER%\*.zip" 2>NULL

echo.
echo "Removing msi files"
del "%TOOLS_FOLDER%\*.msi" 2>NULL

echo.
echo "Removing exe files"
del "%TOOLS_FOLDER%\*.exe" 2>NULL

:: Catch Time when ending installation script
for /f "tokens=1 delims=: " %%h in ('echo %time%') do set end_hour=%%h 2>NULL
for /f "tokens=2 delims=: " %%m in ('echo %TIME%') do set end_minu=%%m 2>NULL
for /f "tokens=3 delims=: " %%s in ('echo %TIME%') do set end_secd=%%s 2>NULL

:: Conversion of both times
set /a hh1=60*60*%start_hour% 2>NULL
set /a hh2=60*60*%end_hour% 2>NULL
set /a mm1=60*%start_minu% 2>NULL
set /a mm2=60*%end_minu% 2>NULL
set /a ss1=%start_secd% 2>NULL
set /a ss2=%end_secd% 2>NULL

:: Add input hours , minutes , seconds 
set /a t1=hh1+mm1+ss1 2>NULL
set /a t2=hh2+mm2+ss2 2>NULL

:: Calculate difference in seconds
set /a ds=t2-t1 2>NULL
if %ds% LSS 0 set /a ds=%ds%+24*60*60 2>NULL

:: Caculate in time format time difference
set /a h=(%ds%/3600) 2>NULL
set /a m=(%ds%/60)-60*%h% 2>NULL
set /a s=%ds%-60*(%ds%/60) 2>NULL

:: Add leading zeros
IF %h% LSS 10 SET h=0%h% 2>NULL
IF %m% LSS 10 SET m=0%m% 2>NULL
IF %s% LSS 10 SET s=0%s% 2>NULL

echo.
echo "Script beginning time (hh:mm:ss) : %start_hour%:%start_minu%:%start_secd%"
echo "Script ending    time (hh:mm:ss) : %end_hour%:%end_minu%:%end_secd%"
echo "Script execution time (hh:mm:ss) : %h%:%m%:%s%"

:: removes NULL file
del %~dp0\NULL

echo "Operations ended successfully... enjoy!"
echo **********************************************************************

cd "%WORKSPACE_FOLDER%\Installation"